package com.mnnu.controller;

import com.mnnu.anno.OperateAnnotation;
import com.mnnu.pojo.Dept;
import com.mnnu.pojo.Result;
import com.mnnu.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping(("/depts"))
public class DeptController {

    @Autowired
    private DeptService deptService;


    @GetMapping
    public Result list() {
        log.info("查询部门信息");
        List<Dept> depts = deptService.list();
        return Result.success(depts);
    }

    @OperateAnnotation
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        log.info("根据id删除部门信息");
        deptService.deleteById(id);
        return Result.success();
    }

    /**
     * 添加部门
     * @param dept
     * @return
     */
    @OperateAnnotation
    @PostMapping
    public Result add(@RequestBody Dept dept) {
        log.info("添加部门");
        deptService.add(dept);
        return Result.success();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        log.info("根据id查询部门");
        Dept dept = deptService.selectById(id);
        return Result.success(dept);
    }

    /**
     * 修改部门信息
     * @param dept
     * @return
     */
    @OperateAnnotation
    @PutMapping
    public Result modify(@RequestBody Dept dept) {
        log.info("修改部门信息");
        deptService.modify(dept);
        return Result.success();
    }


}
